package com.athuahua.r2dbc.converter;

import com.athuahua.r2dbc.entity.TAuthor;
import com.athuahua.r2dbc.entity.TBook;
import io.r2dbc.spi.Row;
import io.r2dbc.spi.RowMetadata;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;

import java.time.LocalDateTime;


/**
 * @Author: huahua
 * @Date: 2024/12/3 0003
 * @Time: 16:06
 * @Description: 自定义转化器
 */
@ReadingConverter
public class BookConverter implements Converter<Row, TBook> {
//把row转换成Tbook
    @Override
    public TBook convert(Row source) {
        //自定义结果集
        TBook tBook = new TBook();

        Long l = source.get("id", Long.class);
        String s = source.get("title", String.class);
        Long l1 = source.get("author_id", Long.class);


        tBook.setId(l);
        tBook.setTitle(s);
        tBook.setAuthorId(l1);
        tBook.setPublishTime(source.get("publish_time", LocalDateTime.class));
        TAuthor tAuthor = new TAuthor();
        tAuthor.setId(l1);
        if(source.getMetadata().contains("name"))
            tAuthor.setName(source.get("name",String.class));


        return tBook;
    }
}
